<template>
  <div class="image-list">
    <div class="head-button">
      <el-radio-group v-model="collect" @change="onCollectchange">
        <el-radio-button :label="false">全部</el-radio-button>
        <el-radio-button :label="true">收藏</el-radio-button>
      </el-radio-group>
      <el-button v-if="uploadImage" type="success" @click="dialogUploadVisible = true">上传素材</el-button>
    </div>
    <el-row :gutter="20">
      <el-col :lg="4" :xs="12" :md="6" :sm="6" v-for="(image,index) in images" :key="image.id" class="image-item" @click.native="onSelected(index)">
        <el-image :src="image.url" fit="cover" style="height: 100px;"></el-image>
        <div class="selected" v-if="showSelected && selected === index"></div>
        <div v-if="showAction" class="image-action">
          <el-button :loading="image.collectLoading" :disabled="image.collectDisable" type="warning" circle size="mini" :icon="image.is_collected ? 'el-icon-star-off' : 'el-icon-star-on'" @click="onCollect(image)"></el-button>
          <el-button :loading="image.deleteLoaing" :disabled="image.deleteDisable" type="danger" circle size="mini" icon="el-icon-delete" @click="onDelete(image)"></el-button>
        </div>
      </el-col>
    </el-row>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount"
      :page-size="pageSize"
      :current-page.sync="currentPage"
      @current-change="onCurrentChange">
    </el-pagination>
    <el-dialog title="上传素材" :visible.sync="dialogUploadVisible" :append-to-body="true" width="25%">
      <div class="upload">
        <el-upload
          v-if="dialogUploadVisible"
          drag
          action="http://api-toutiao-web.itheima.net/mp/v1_0/user/images"
          :headers=uploadHeaders
          name="image"
          :on-success="onUploadSuccess"
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将素材拖到此处，或<em>点击上传</em></div>
          <!-- <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div> -->
        </el-upload>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getImages, collectImage, deleteImage } from '@/api/image.js'
export default {
  name: 'ImageList',
  components: {},
  props: {
    uploadImage: {
      type: Boolean,
      default: true
    },
    showAction: {
      type: Boolean,
      default: true
    },
    pageSize: {
      type: Number,
      default: 24
    },
    showSelected: {
      type: Boolean,
      default: false
    }
  },
  data () {
    const user = JSON.parse(window.localStorage.getItem('user'))
    return {
      collect: false,
      images: [],
      totalCount: 0,
      // pageSize: 24,
      currentPage: 1,
      dialogUploadVisible: false,
      uploadHeaders: {
        Authorization: `Bearer ${user.token}`
      },
      selected: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadImages()
  },
  mounted () {},
  methods: {
    loadImages () {
      getImages({
        collect: this.collect,
        page: this.currentPage,
        per_page: this.pageSize
      }).then(res => {
        const results = res.data.data.results
        results.forEach(img => {
          img.collectLoading = false
          img.collectDisable = false
          img.deleteLoaing = false
          img.deleteDisable = false
        })
        this.images = results
        this.totalCount = res.data.data.total_count
      })
    },
    onCurrentChange (page) {
      this.loadImages()
    },
    onCollectchange (value) {
      this.collect = value
      this.currentPage = 1
      this.loadImages()
    },
    onUploadSuccess (response, file, fileList) {
      this.dialogUploadVisible = false
      this.collect = false
      this.currentPage = 1
      this.loadImages()
    },
    onCollect (image) {
      image.collectLoading = true
      image.deleteDisable = true
      collectImage(image.id, !image.is_collected).then(res => {
        image.is_collected = !image.is_collected
        image.collectLoading = false
        image.deleteDisable = false
      })
    },
    onDelete (image) {
      image.deleteLoaing = true
      image.collectDisable = true
      deleteImage(image.id).then(res => {
        image.deleteLoaing = false
        this.loadImages()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      })
    },
    onSelected (index) {
      this.selected === index ? this.selected = null : this.selected = index
    }
  }
}

</script>
<style lang='less' scoped>
.head-button {
  display: flex;
  justify-content: space-between;
  padding-bottom: 20px;
}
.upload {
  display: flex;
  justify-content: center;
}
.image-item {
  position: relative;
  margin-bottom: 30px;
  position: relative;
}
.selected {
  background: url('./selected.png') no-repeat;
  width: 120px;
  height: 120px;
  background-size: cover;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
.image-action {
  font-size: 20px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  color: #ffffff;
  height: 30px;
  background-color: rgba(204, 204, 204, .5);
  position: absolute;
  bottom: 4px;
  left: 10px;
  right: 10px;
}
</style>
